<?php
namespace app\api\controller;

use think\Controller;

use \payment\Weixin;

use app\index\model\Payorder;
use \wxpay\JsApiPay;

class Wxpay extends Controller
{
    public function text(){
        $txt = file_get_contents(env('root_path').'data/minganci.txt');
        $txt = str_replace(PHP_EOL,"%",$txt);  ##同上.
        // $content = iconv('gb2312', "utf-8", $txt);  
        echo $txt;
    }

    /**
     * 微信订单notify_url处理
     */
    public function notify(){
        $xml = file_get_contents('php://input');
        $data = FromXml($xml);
        if($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS'){
            $wx = new Weixin;
            $xmlSign = $data['sign'];
            $sign = $wx->MakeSign($data);
            if($sign == $xmlSign){
                $order = Payorder::where('trade_no', $data['out_trade_no'])->find();
                if(!empty($order) && $order->price == $data['total_fee']){
                    $order->bank_type       = $data['bank_type'];
                    $order->time_end        = $data['time_end'];
                    $order->transaction_id  = $data['transaction_id'];
                    $order->status          = 1;
                    $order->save();
                    return xml('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                }
            }
        }
    }
}